﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using IssueCommon;
using ValidateCommon;
using Common;
namespace RichECMS.ServerPages.Section
{
    public partial class AddSection : Base.SectionBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (State.LoginUser.ManagerCompetence > 2)
                {
                }
                else
                {
                    if (State.LoginUser.EnableParentPath)
                    {
                        trPath.Visible = true;
                        trPathInfo.Visible = true;
                    }
                    else
                    {
                        trPath.Visible = false;
                        trPathInfo.Visible = false;
                    }
                }

                if (Request["section"] != null)
                {
                    if (!ParamAuth.IsNumber(Request["section"].ToString().Trim()))
                    {
                        Alert("参数错误");
                        return;
                    }
                }
                txtDateInTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                InitDataBind();
                if (AppConfig.GetSectionReferEnabled())
                {
                    this.trReferOn.Visible = true;
                    MainSectionBind();
                }
                else
                {
                    this.trReferOn.Visible = false;
                }
            }
        }

        protected void MainSectionBind()
        {
            try
            {
                //绑定文章类型
                DropDownListMainSectionlist.Items.Clear();
                ListItem listitem = new ListItem("--请选择板块--", "-1");
                DropDownListMainSectionlist.Items.Add(listitem);
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.CreatorBySql("intParentID =0 and intWSID=" + State.intWSID + " order by intSortNo desc,  dtmInTime desc");
                    if (objsection != null)
                    {
                        for (int i = 0; i < objsection.Count; i++)
                        {
                            listitem = new ListItem(objsection.DataItem.chvSTCName + (objsection.DataItem.IstxtRemarksNull() ? "" : "(" + objsection.DataItem.txtRemarks + ")"),
                                                    objsection.DataItem.intSectionID.ToString());
                            DropDownListMainSectionlist.Items.Add(listitem);
                            objsection.SelectRow(i + 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 页面输出
        /// </summary>
        private void InitDataBind()
        {
            //绑定文章类型
            ListItem listitem = new ListItem("--请选择文章模版--", "-1");
            DropDownListArticle.Items.Add(listitem);
            using (TemplateFactory objFactory = new TemplateFactory())
            {
                BusinessLogic.BusinessRules.Template objTemplate = objFactory.CreatorBySql(" intType=" + (int)Utility.TemplateType.Article + " and intWSID=" + State.intWSID);
                if (objTemplate != null)
                {
                    for (int i = 0; i < objTemplate.Count; i++)
                    {
                        listitem = new ListItem(objTemplate.DataItem.chvName, objTemplate.DataItem.intTemplateID.ToString());
                        DropDownListArticle.Items.Add(listitem);
                        objTemplate.SelectRow(i + 1);
                    }

                    //默认选中空白
                    DropDownListArticle.SelectedIndex = 1;
                }

                //绑定栏目类型
                listitem = new ListItem("--请选择栏目模版--", "-1");
                DropDownListSection.Items.Add(listitem);
                objTemplate = objFactory.CreatorBySql(" intType=" + (int)Utility.TemplateType.Section + " and intWSID=" + State.intWSID);
                if (objTemplate != null)
                {
                    for (int i = 0; i < objTemplate.Count; i++)
                    {
                        listitem = new ListItem(objTemplate.DataItem.chvName, objTemplate.DataItem.intTemplateID.ToString());
                        DropDownListSection.Items.Add(listitem);
                        objTemplate.SelectRow(i + 1);
                    }

                    //默认选中空白
                    DropDownListSection.SelectedIndex = 1;
                }
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitleCN.Value.Trim().Length == 0)
            {
                Alert("请填写栏目中文名称");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(txtDateInTime.Text)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }
            if (DropDownListSection.SelectedValue.Trim() == "-1")
            {
                Alert("请选择栏目模版");
                bolIsValidate = false;
            }

            if (this.txtSortNo.Value.Trim().Length == 0)
            {
                Alert("请填写排序数据");
                bolIsValidate = false;
            }
            else
            {
                if (!ParamAuth.IsNumber(this.txtSortNo.Value.Trim()))
                {
                    Alert("请输入正确的数字格式");
                    bolIsValidate = false;
                }
            }

            if (CKBoxReferSection.Checked)
            {
                if (!ParamAuth.IsNumber(this.ChangeSecHidden.Value.Trim()))
                {
                    Alert("请选择要引用的栏目");
                    bolIsValidate = false;
                }
            }

            if (State.LoginUser.EnableParentPath)
            {
                using (SectionsFactory objSectionsFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objSections = objSectionsFactory.CreatorBySql(" chvParentPath ='" + this.txtPerentPath.Text.Trim().Replace("'", "") + "' and chvPageName ='" + this.txtPageName.Value.Trim().Replace("'", "") + "'");
                    if (objSections != null)
                    {
                        Alert("该父路径和文件名已经被占用");
                        bolIsValidate = false;
                    }
                }
            }

            //if (DropDownListArticle.SelectedValue.Trim() == "-1")
            //{
            //    Alert("请选择文章模版");
            //    bolIsValidate = false;
            //}
            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                if (CKBoxReferSection.Checked)
                {
                    this.trReferSelect.Visible = false;
                    using (SectionsFactory objFactory = new SectionsFactory())
                    {
                        try
                        {
                            BusinessLogic.BusinessRules.Sections objSections = objFactory.Creator(Convert.ToInt32(ChangeSecHidden.Value));
                            if (objSections != null)
                            {
                                LitReferSectionName.Text = objSections.DataItem.chvSTCName+"(ID:"+objSections.DataItem.intSectionID+")";
                            }
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.Message.Replace("\r\n", ""));
                        }
                    }
                }
                return;
            }
            using (CommentFactory objFactory = new CommentFactory())
            {
                BusinessLogic.BusinessRules.Comment objSections = objFactory.NewInstance();
            }
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSections = objFactory.NewInstance();
                objSections.DataItem.chvSTCName = this.txtTitleCN.Value;
                objSections.DataItem.chvSTEName = this.txtTitleEn.Value.Trim().Equals("") ? Common.HzToPy.getSpell(this.txtTitleCN.Value.Trim()) : this.txtTitleEn.Value;
                objSections.DataItem.intParentID = Request["section"] != null ? Convert.ToInt32(Request["section"].ToString().Trim()) : 0;
                objSections.DataItem.intArticleTemID = Convert.ToInt32(DropDownListArticle.SelectedValue.Trim());
                objSections.DataItem.intSectionTemID = Convert.ToInt32(DropDownListSection.SelectedValue.Trim());
                objSections.DataItem.intState = Convert.ToInt32(RadioButtonListArticle.SelectedValue.Trim());
                objSections.DataItem.intType = Convert.ToInt32(RadioButtonListSection.SelectedValue.Trim());
                objSections.DataItem.dtmInTime = Convert.ToDateTime(txtDateInTime.Text);
                objSections.DataItem.txtRemarks = txtRemark.Text;
                objSections.DataItem.intWSID = State.intWSID;
                objSections.DataItem.txtSTMemo = "";
                objSections.DataItem.chvContent = "";
                objSections.DataItem.intDisplay = 1;
                objSections.DataItem.intSortNo = Convert.ToInt32(txtSortNo.Value.Trim());
                objSections.DataItem.chvSectionMainPic = "";
                objSections.DataItem.chvFullPath = "";
                objSections.DataItem.chvDescription = " ";
                objSections.DataItem.txtSeodescription = " ";
                objSections.DataItem.txtSeoKeywords = " ";
                objSections.DataItem.txtSeoTitle = " ";
                objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                objSections.DataItem.chvDataLabel = RebuildDataLabel();
                objSections.DataItem.chvDataModel = "";
                objSections.DataItem.chvDataPrototype = "";
                //发布路径设置
                objSections.DataItem.chvParentPath = this.txtPerentPath.Text.Trim().Replace("'", "");
                objSections.DataItem.chvPageName = this.txtPageName.Value.Trim().Replace("'", "");
                //设置栏目引用
                if (CKBoxReferSection.Checked)
                {
                    objSections.DataItem.intStateRefer = (int)Common.Utility.StateRefer.Yes;
                    int intReferSectionID = -1;
                    BusinessLogic.BusinessRules.Sections.FindSectionReferRoot(Convert.ToInt32(ChangeSecHidden.Value), ref intReferSectionID);
                    objSections.DataItem.intReferSectionID = intReferSectionID;
                }
                else
                {
                    objSections.DataItem.intStateRefer = (int)Common.Utility.StateRefer.No;
                    objSections.DataItem.intReferSectionID = -1;
                }
                objSections.Insert();
            }
            AlertAndRedirectAndExecOther("新增栏目成功！即将跳转，请稍候", "SectionsManage.aspx", false, "window.parent.refrashleft();");
        }

        protected void SetRefer(object sender, EventArgs e)
        {
            if (this.CKBoxReferSection.Checked == true)
            {
                this.trReferSelect.Visible = true;
                this.spanReferSectionName.Visible = true;
            }
            else
            {
                this.spanReferSectionName.Visible = false;
                this.trReferSelect.Visible = false;
            }
        }
        protected void ReferSectionReselect(object sender, EventArgs e)
        {
            this.trReferSelect.Visible = true;
        }
    }
}